﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Ormon.HostLinkLib.CmodeBase
{
    internal class CmodeEndCode
    {
        /// <summary>
        /// 对比回复帧中的EndCode，看内容是否相符
        /// </summary>
        /// <param name="ResponseFrame">回复帧</param>
        /// <returns>返回结束代码</returns>
        public static string CatchEndCode(List<byte> ResponseFrame, int EndCodeAdr)
        {
            try
            {
                List<byte> ResponseEndCode = ResponseFrame.GetRange(EndCodeAdr, 2);
                string EndCodeContents = null;

                if (ResponseEndCode.SequenceEqual(new List<byte> { 0x30, 0x30 }))
                {
                    EndCodeContents = "00";
                }
                else
                {
                    foreach (var EndCodeMap in EndCodeMapSeq)
                    {
                        if (ResponseEndCode.SequenceEqual(EndCodeMap.Key))
                        {
                            EndCodeContents = EndCodeMap.Value;
                            break;
                        }
                    }
                }
                return EndCodeContents;
            }
            catch (Exception)
            {
                throw;
            }
        }


        private static readonly Dictionary<List<byte>, string> EndCodeMapSeq = new Dictionary<List<byte>, string>
        {
            {new List<byte> { 0x30, 0x31 },"EndCode: 01;  Contents: Not executable in RUN mode" },
            {new List<byte> { 0x30, 0x32 },"EndCode: 02;  Contents: Not executable in MONITOR mode" },
            {new List<byte> { 0x30, 0x33 },"EndCode: 03;  Contents: UM write-protected" },
            {new List<byte> { 0x30, 0x34 },"EndCode: 04;  Contents: Address over" },
            {new List<byte> { 0x30, 0x42 },"EndCode: 0B;  Contents: Not executable in PROGRAM mode" },
            {new List<byte> { 0x31, 0x33 },"EndCode: 13;  Contents: FCS error" },
            {new List<byte> { 0x31, 0x34 },"EndCode: 14;  Contents: Format error" },
            {new List<byte> { 0x31, 0x35 },"EndCode: 15;  Contents: Entry number data error" },
            {new List<byte> { 0x31, 0x36 },"EndCode: 16;  Contents: Command not supported" },
            {new List<byte> { 0x31, 0x38 },"EndCode: 18;  Contents: Frame length error" },
            {new List<byte> { 0x31, 0x39 },"EndCode: 19;  Contents: Not executable" },
            {new List<byte> { 0x32, 0x30 },"EndCode: 20;  Contents: Could not create I/O table" },
            {new List<byte> { 0x32, 0x31 },"EndCode: 21;  Contents: Not executable due to CPU Unit CPU error" },
            {new List<byte> { 0x32, 0x33 },"EndCode: 23;  Contents: User memory protected" },
            {new List<byte> { 0x41, 0x33 },"EndCode: A3;  Contents: Aborted due to FCS error in trans-mission data" },
            {new List<byte> { 0x41, 0x34 },"EndCode: A4;  Contents: Aborted due to format error in transmission data" },
            {new List<byte> { 0x41, 0x35 },"EndCode: A5;  Contents: Aborted due to entry number data error in transmission data" },
            {new List<byte> { 0x41, 0x38 },"EndCode: A8;  Contents: Aborted due to frame length error in transmission data" }
        };
    }
}
